<?php
# 文件名称:feedback.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class feedbackModel extends C_model {
	var $_lists   = array();
	
	var $_pagination = null;
	
    function feedbackModel()
    {
        parent::C_model();
    }
	//表单的主题风格
	function getMailTpls()
	{
		$SYS =& get_instance();
		$CFG = &$SYS->config;
		$path = PATH_THEMES.DS.$CFG->item('theme').DS.'mailtpls';
		//目录	
		$fso    =   opendir($path);
		$result = 	array();
		$result[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' -', 'value', 'text');
		if($fso)
		{
			while($file=readdir($fso))
			{
				if($file!='.' && $file!='..')
				{			
					$filepath=$path.DS.$file;
					//处理目录
					if(is_file($filepath)) 
					{											
						$result[] = C_html::_('select.option',  $file, $file);						
					}					
				}				
			}
		}		
		$result = C_html::_('select.genericlist',   $result, 'mailtpl', ' class="inputbox"  ', 'value', 'text' );
		return $result;
	}
	//单个表单字段
	function getItem()
	{
		if (!empty( $this->_one )) 
		{
			return $this->_one;
		}
		$SYS =&get_instance();
		//参数过滤
		$id = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		//数据
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'feedback'.
				' WHERE id='.$id[0]
				;
		$this->_db->setQuery($query);
		$this->_one = $this->_db->loadAssoc();
		return $this->_one;
	}
	//反馈数据集
	function getItems()
	{
		if (!empty( $this->_data ))
		{
			return $this->_data;
		}
		$SYS =&get_instance();
		// 获取请求的数据
		$filter_form		= $SYS->request->getVar( "filter_form",		'-1',		'post',	'int' );
		$filter_order		= $SYS->request->getVar( "filter_order",		'a.addtime',		'post',	'cmd' );
		$filter_order_Dir	= $SYS->request->getVar( "filter_order_Dir",	'desc',	'post',			'word' );
		$filter_lists	= $SYS->request->getVar( 'filter_lists',	0,	'post',	'int' );
		$filter_list	= $SYS->request->getVar( 'filter_list',	0,	'post',	'int' );
		if($filter_form == 0) 
		{
			$filter_lists=0;
			$filter_list=0;
		}
		$search				= $SYS->request->getVar( "search",			'', 			'post',			'string' );
		$limit = $SYS->request->getInt( 'limit',10 );
		$limitstart = $SYS->request->getInt( 'limitstart' );
		
		if($filter_form == -1) $filter_form = $SYS->request->getVar( "fdparameter_form",	'0', 'COOKIE',	'int' );
		else
		{		
			if($filter_form != $SYS->request->getVar( "fdparameter_form",	'0', 'COOKIE',	'int' ))
			{
				$limitstart = 0;
			}
		}
		setcookie('fdparameter_form', $filter_form, time()+$SYS->config->item('lifetime'), '/');
		//过滤查询
		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
			$where[] = 'a.fdtitle LIKE '.$searchEscaped;
		}
		//权限
		$currentUser = & $SYS->session->get('admin_name');
		$defaultlang=$SYS->lang->getDefault(1,'backend');
		if(!in_array("1",explode(",",$currentUser->get('gid'))))
		{
			$condition = 'a.created_by='.$currentUser->get('id');
			$query = 'SELECT b.fieldvalue name'.
					' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
					' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
					' WHERE '.$condition.
					' AND a.id=b.recordid'.
					' AND b.module=1 AND b.fieldname="name"'.
					' AND b.lang='.$defaultlang['id']
					;
			$tmpformtitle = $this->_getArrayList($query);
			$tmptitle=array();
			for($i=0;$i<count($tmpformtitle);$i++)
			{
				$tmptitle[]= '"'.$tmpformtitle[$i]['name'].'"';				
			}
			$where[] = 'a.fdtitle IN ('.implode(",", $tmptitle).')';
			
		}else $condition = '1=1';
		//筛选下拉菜单
		
		//各类表单
		if($filter_form != 0)
		{
			
			$query = 'SELECT b.fieldvalue'.
					' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
					' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
					' WHERE a.id='.$filter_form.
					' AND '.$condition.
					' AND a.id=b.recordid'.
					' AND b.module=1 AND b.fieldname="name"'.
					' AND b.lang='.$defaultlang['id']
					;
			$this->_db->setQuery($query);
			$form = $this->_db->loadResult();
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $form, true ).'%', false );
			$where[] = 'a.fdtitle LIKE '.$searchEscaped;
		}
		//各个字段参数
		if($filter_lists!=0 && $filter_list!=0)
			$query = 'SELECT b.fieldvalue `key`, d.fieldvalue `value`'.
					' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
					' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].')'.
					' ,'.$this->_db->_table_prefix.'fdlist'.' AS c '.
					' LEFT JOIN '.$this->_db->_table_prefix.'fields AS d ON ( d.module =4 AND d.recordid = c.id AND d.lang='.$defaultlang['id'].' )'.
					' WHERE a.id='.$filter_lists.' AND c.id='.$filter_list
					;
		else
			$query = 'SELECT b.fieldvalue `key`'.
					' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
					' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].')'.
					' WHERE a.id='.$filter_lists
					;
		
		$this->_db->setQuery($query);
		$parameter = $this->_db->loadObject();
		//相应下拉筛选
		if($parameter != null)
		{
			if($filter_lists!=0 && $filter_list==0) $parameter = serialize($parameter->key);
			else $parameter = serialize($parameter);
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $parameter, true ).'%', false );
			$where[] = 'a.data like '.$searchEscaped;
		}
		
		
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		
		//分页
		_import('html/pagination',false);
		$query = 'SELECT COUNT(*)' .
				' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
				$where.
				$orderby;
		$this->_db->setQuery($query);
		$total = $this->_db->loadResult();
		$this->_pagination = new C_pagination( $total, $limitstart, $limit );		
		
		// 下拉菜单
		$query = 'SELECT a.id,b.fieldvalue `name`' .
				' FROM '.$this->_db->_table_prefix.'fdparameter'.' AS a '.
				' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =3 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].')'.
				' WHERE a.use_ok=1 AND (a.type=2 OR a.type=4 OR a.type=6)'.
				' AND a.form='.$filter_form
				;
		//字段下拉框
		$types[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('Parameter').' -', 'id', 'name');
		$this->_db->setQuery($query);
		$types = array_merge($types, $this->_db->loadObjectList());
		$this->_lists['types'] = C_html::_('select.genericlist',  $types, 'filter_lists', 'class="inputbox" size="1" onchange="if(document.getElementById(\'limitstart\')) document.getElementById(\'limitstart\').value=0;document.adminForm.submit( );"', 'id', 'name', $filter_lists);
		
		$query = 'SELECT a.id,b.fieldvalue `name`' .
				' FROM '.$this->_db->_table_prefix.'fdlist'.' AS a '.
				' LEFT JOIN '.$this->_db->_table_prefix.'fields AS b ON ( b.module =4 AND b.recordid = a.id AND b.lang='.$defaultlang['id'].' )'.
				' WHERE bigid='.$filter_lists;
		$type[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('List').' -', 'id', 'name');
		$this->_db->setQuery($query);
		$type = array_merge($type, $this->_db->loadObjectList());
		//列表下拉框
		$this->_lists['type'] = C_html::_('select.genericlist',  $type, 'filter_list', 'class="inputbox" size="1" onchange="if(document.getElementById(\'limitstart\')) document.getElementById(\'limitstart\').value=0;document.adminForm.submit( );"', 'id', 'name', $filter_list);
		$forms[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('Form').' -', 'id', 'name');
				
		$query = 'SELECT a.id,b.fieldvalue name'.
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' WHERE b.module=1 AND b.fieldname="name" AND b.lang='.$defaultlang['id'].
				' AND a.id=b.recordid '.
				' AND '.$condition;
				
		$forms = array_merge($forms, $this->_getObjectList( $query));
		//表单下拉框
		$this->_lists['form'] = C_html::_('select.genericlist',  $forms, "filter_form", 'class="inputbox" onchange="if(document.getElementById(\'limitstart\')) document.getElementById(\'limitstart\').value=0;document.adminForm.submit( );"', 'id', 'name', $filter_form);
		
		//数据集
		$query = 'SELECT *' .
				' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
				$where.
				$orderby;		
		$this->_data = $this->_getArrayList( $query, $this->_pagination->limitstart, $this->_pagination->limit );
		$this->_query = 'SELECT *' .
						' FROM '.$this->_db->_table_prefix.'feedback'.' AS a '.
						$where.
						' ORDER BY a.fdtitle,a.number';
		$this->_lists['order_Dir']	= $filter_order_Dir;
		$this->_lists['order']		= $filter_order;
		$this->_lists['search']		= $search;
		return $this->_data;
	}
	//排序
	function getLists()
	{		
		return $this->_lists;
	}
	//分页
	function getPagination()
	{
		return $this->_pagination;
	}
	
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>